package com.cande.loki.exercise.algorithm;

import java.util.LinkedHashMap;
import java.util.Map;

public class LRUTest<K,V> extends LinkedHashMap<K,V> {
    /**
     * 使用LinkedHashMap 重写removeEldestEntry方法 简单实现
     */

    private int capacity;

    public LRUTest(int capacity) {
        super(capacity,0.5f,true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > capacity;
    }

    public static void main(String[] args) {
        LRUTest<Integer, String> cache = new LRUTest<>(8);
//        HashMap cache = new HashMap<>(5,0.5f);

        cache.put(1, "one");
        cache.put(2, "two");
        cache.put(3, "three");
        cache.put(4, "three");
        cache.put(5, "three");
        cache.put(6, "three");
        cache.put(7, "three");
        cache.put(8, "three");
        cache.put(9, "three");
        cache.put(10, "three");
        cache.put(11, "three");
        cache.put(12, "three");

        System.out.println(cache); // 输出: {1=one, 2=two, 3=three}

        cache.get(1); // 访问键1，将其移到最近使用的位置
        cache.put(4, "four"); // 插入新元素，将导致键2被移除

        System.out.println(cache); // 输出: {3=three, 1=one, 4=four}

        cache.put(5, "five"); // 再次插入新元素，将导致键3被移除

        System.out.println(cache); // 输出: {1=one, 4=four, 5=five}
    }
}

class LRU{

}
